package com.sunshine.homepage.service.impl;

import com.sunshine.homepage.dao.ProductsDao;
import com.sunshine.homepage.entity.Products;
import com.sunshine.homepage.service.homepageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class homepageServiceImpl implements homepageService {
    
    @Resource
    private ProductsDao productsDao;
    


    @Override
    public Result<List<Products>> findAllProducts(int page, int size) {
        //无条件分页查询，只查询上架状态（status=1）且库存大于0的商品
        Pageable pageable = PageRequest.of(page, size);
        Products products = new Products();
        // 设置只查询上架商品
        products.setStatus(1);
        // 设置只查询库存大于0的商品
        products.setStock(1);
        List<Products> products1 = productsDao.queryAllByLimit(products, pageable);
        log.info("查询所有上架且库存大于0的商品: {}", products1);
        return Result.ok(products1);
    }

    @Override
    public Result<List<Products>> findProductsByCategory(String category) {
        //根据分类名查询，只查询上架状态（status=1）且库存大于0的商品
        List<Products> productsList = productsDao.findByCategory(category);
        // 过滤掉库存不足的商品
        productsList.removeIf(product -> product.getStock() == null || product.getStock() <= 0);
        log.info("根据分类名查询商品: 分类={}, 结果数量={}", category, productsList.size());
        return Result.ok(productsList);
    }
}